package com.sugar.follow.up.module.business.service.patientfszl;

import com.sugar.follow.up.module.business.controller.admin.patient.fszl.vo.PatientFszlPageReqVO;
import com.sugar.follow.up.module.business.controller.admin.patient.fszl.vo.PatientFszlSaveReqVO;
import com.sugar.follow.up.module.system.api.user.AdminUserApi;
import com.sugar.follow.up.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.sugar.follow.up.module.business.dal.dataobject.patientfszl.PatientFszlDO;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;

import com.sugar.follow.up.module.business.dal.mysql.patientfszl.PatientFszlMapper;

import static com.sugar.follow.up.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.PATIENT_FSZL_NOT_EXISTS;

/**
 * 放射治疗 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class PatientFszlServiceImpl implements PatientFszlService {

    @Resource
    private PatientFszlMapper patientFszlMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createPatientFszl(PatientFszlSaveReqVO createReqVO, Long adminUserId) {
        // 获取医生
        AdminUserRespDTO user = adminUserApi.getUser(adminUserId);

        // 插入
        PatientFszlDO patientFszl = BeanUtils.toBean(createReqVO, PatientFszlDO.class);
        patientFszl.setDoctorId(adminUserId).setDoctorName(user.getNickname());
        patientFszlMapper.insert(patientFszl);

        // 返回
        return patientFszl.getId();
    }

    @Override
    public void updatePatientFszl(PatientFszlSaveReqVO updateReqVO) {
        // 校验存在
        validatePatientFszlExists(updateReqVO.getId());
        // 更新
        PatientFszlDO updateObj = BeanUtils.toBean(updateReqVO, PatientFszlDO.class);
        patientFszlMapper.updateById(updateObj);
    }

    @Override
    public void deletePatientFszl(Long id) {
        // 校验存在
        validatePatientFszlExists(id);
        // 删除
        patientFszlMapper.deleteById(id);
    }

    @Override
        public void deletePatientFszlListByIds(List<Long> ids) {
        // 删除
        patientFszlMapper.deleteByIds(ids);
        }


    private void validatePatientFszlExists(Long id) {
        if (patientFszlMapper.selectById(id) == null) {
            throw exception(PATIENT_FSZL_NOT_EXISTS);
        }
    }

    @Override
    public PatientFszlDO getPatientFszl(Long id) {
        return patientFszlMapper.selectById(id);
    }

    @Override
    public PageResult<PatientFszlDO> getPatientFszlPage(PatientFszlPageReqVO pageReqVO) {
        return patientFszlMapper.selectPage(pageReqVO);
    }

}